#region license
/*
 * Copyright (C) 2007 CchenSoft.com
 * 
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License 2.1 or later, as
 * published by the Free Software Foundation. See the included License.txt
 * or http://www.gnu.org/copyleft/lesser.html for details.
 * 
 * Author: Billy Zhang
 */
#endregion

using System;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
using CchenSoft.Workflow.Spi;
using System.Collections;

namespace CchenSoft.Workflow.Util.Python
{
    public class BeanShellRegister : IRegister
    {
        //~ Methods ////////////////////////////////////////////////////////////////

        public object RegisterVariable(IWorkflowContext context, IWorkflowEntry entry, IDictionary args)
        {
            string script = (string)args[WorkflowConstants.BSH_SCRIPT];

            try
            {
                Hashtable vars = new Hashtable();
                vars.Add("entry", entry);
                vars.Add("context", context);
                vars.Add("model", context.Model);

                return PythonHelper.ExecuteScript(script, vars);
            }
            catch (Exception ex)
            {
                if (ex.InnerException is WorkflowException)
                {
                    throw (WorkflowException)ex.InnerException;
                }
                else
                {
                    String message = "Could not get object registered in to variable map";
                    throw new WorkflowException(message, ex.InnerException);
                }
            }
            finally
            {
            }
        }
    }
}
